package cn.jbolt.core.permission;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.JBoltApiKit;
import cn.jbolt.core.base.JBoltMsg;
import cn.jbolt.core.kit.JBoltControllerKit;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

/**
 * JBolt API权限鉴权拦截器
 * 当接口是jb_user登录授权后调用的 需要CheckPermission和Login之类校验时用
 * 可检测jb_user是否登录，可检测登录用户身上是否存在CheckPermission声明权限或者Login注解
 */
public class JBoltApiAuthInterceptor implements Interceptor {
	@Override
	public void intercept(Invocation inv) {
		Controller ctl =inv.getController();
		if (!(ctl instanceof JBoltApiBaseController)) {
			throw new RuntimeException(JBoltMsg.INTERCEPTOR_MUST_EXTEND_JBOLT_API_BASE_CONTROLLER);
		}
		JBoltApiBaseController controller=(JBoltApiBaseController) ctl;
		//是否 jbuser
		boolean isSystemUser = JBoltApiKit.isSystemUser();
		if (!isSystemUser) {
			JBoltControllerKit.renderAdminInterceptorNotLoginInfo(controller);
			return;
		}
		//执行处理
		JBoltAuthInterceptorKit.intercept(inv);
	}
}
